package se.energimyndigheten.lampguiden.bulbreplacement;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.DashboardActivity;
import se.energimyndigheten.lampguiden.LightSource;
import se.energimyndigheten.lampguiden.bulbreplacement.SetPriceDialogFragment.onPriceChangedListener;

public class ResultListFragment extends ListFragment {

    public static ResultListFragment newInstance() {
        ResultListFragment frag = new ResultListFragment();
        return frag;
    }
    
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.result_list, container, false);
        return view;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LightSource lights[] = ((onPriceChangedListener) getActivity()).getLightSources();
        setListAdapter(new ResultAdapter(getActivity(), R.layout.result_listitem, lights));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    
    public void handleItemSelection(View v) {
        View  btn = v.findViewById(R.id.btn_1);
        LightSource light = (LightSource) btn.getTag();
        String document = light.getDocument();
        Intent intent = new Intent(getActivity(), ExplainActivity.class);
        String EXPLAIN_PATH = DashboardActivity.HTML_BASEPATH + "Lamptyper/sv_"+document+".html";
        intent.putExtra(ExplainActivity.EXTRA_HTML_PATH, EXPLAIN_PATH);
        intent.putExtra(ExplainActivity.EXTRA_TITLE, light.getName());
        startActivity(intent);
    }
    
    public class ResultAdapter extends ArrayAdapter<LightSource> {
        Context mContext;
        LightSource[] mData;
        int mLayout;
        Boolean virgin=true;

        public ResultAdapter(Context context, int layoutResourceId, LightSource[] objects) {
            super(context, layoutResourceId, objects);
            mContext = context;
            mData = objects;
            mLayout = layoutResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;

            if (row == null) {
                LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                row = inflater.inflate(mLayout, parent, false);
            }
            
            LightSource light = mData[position];
            Button changeButton = (Button) row.findViewById(R.id.btn_1);
            changeButton.setTag(light);
            if (virgin &&  getListView().isInTouchMode()) {
                changeButton.requestFocus();
                changeButton.requestFocusFromTouch();
                virgin=false;
            }
            
            row.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleItemSelection(v);
                }
            });
            
            TextView title = (TextView) row.findViewById(R.id.title);
            int drawable = selectDrawable(light.getEnergyClass());
            title.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable, 0);
            title.setContentDescription(drawableDescription(drawable));
            ((TextView) row.findViewById(R.id.title)).setText(light.getName());
            ((TextView) row.findViewById(R.id.label_1)).setText(getResources().getString(
                    R.string.result_price_text,
                    light.getMyPrice()));
            int savings = ((onPriceChangedListener) getActivity()).calculateSavings(light, -1);

            if (savings<0)
                savings=0; // Hide losses
            
            ((TextView) row.findViewById(R.id.label_3)).setText(getResources().getString(
                    R.string.result_savings_value,
                    savings));

            String msg="";
            if (savings > 0)
                msg = getResources().getString( R.string.result_savings_years,
                        light.getYears());
            
            ((TextView) row.findViewById(R.id.years)).setText(msg);

            return row;
        }

        private int selectDrawable(String type) {
            if ("A++-A".equals(type))
                return R.drawable.ic_energi_aplusplus_a;
            if ("A+-A".equals(type))
                return R.drawable.ic_energi_aplus_a;
            if ("B".equals(type))
                return R.drawable.resultat_b;
            if ("C".equals(type))
                return R.drawable.resultat_c;
            if ("C-D".equals(type))
                return R.drawable.resultat_cd;
            return 0;
        }
        
        private String drawableDescription(int  d) {
            int stringid=0;
            switch (d) {
                case R.drawable.ic_energi_aplusplus_a:
                    stringid = R.string.ic_energi_aplusplus_a;
                    break;
                case R.drawable.ic_energi_aplus_a:
                    stringid = R.string.ic_energi_aplus_a;
                    break;
                case R.drawable.resultat_b:
                    stringid = R.string.resultat_b;
                    break;
                case R.drawable.resultat_c: 
                    stringid = R.string.resultat_c;
                    break;
                case R.drawable.resultat_cd: 
                    stringid = R.string.resultat_cd;
                    break;
            }
            if (stringid==0)
                return null;
            return getActivity().getResources().getString(stringid);
        }
    }
}